package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.MenuService;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 */

@RestController
@RequestMapping("permission")
public class PermissionController {

    @Reference
    private MenuService menuService;

    @Reference
    private PermissionService permissionService;


    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult menuList = permissionService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, "获取权限列表成功", menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取权限列表失败");
        }
    }

    @RequestMapping("findPermissionById")
    public Result findPermissionById(@RequestParam("id") Integer id) {
        try {
            Permission permission = permissionService.findPermissionById(id);
            return new Result(true, "查询权限成功", permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询权限失败");
        }
    }

    @RequestMapping("addPermission")
    public Result addPermission(@RequestBody Permission permission) {
        try {
            permissionService.addPermission(permission);
            return new Result(true, "添加权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加权限失败");
        }
    }

    @RequestMapping("deletePermission")
    public Result deletePermission(@RequestParam("id") Integer id) {
        try {
            permissionService.deletePermission(id);
            return new Result(true, "删除权限成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除权限失败");
        }
    }


    @RequestMapping("findPermissionByTableData")
    public Result findPermissionByTableData() {
        try {
            List<Permission> permissions = permissionService.findPermissionByTableData();
            return new Result(true, "查询权限列表成功", permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询权限列表失败");
        }
    }

    @RequestMapping("findpermissionByRoleId")
    public Result findpermissionByRoleId(@RequestParam("id") Integer id) {
        try {
            List<Integer> count = permissionService.findPermissionByRoleId(id);
            return new Result(true, "查询引用权限成功", count);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询引用权限失败");
        }
    }

    @RequestMapping("findPermissionAll")
    public Result findPermissionAll() {
        try {
            List<Permission> permissionList = permissionService.findPermissionAll();

            return new Result(true, "查询权限列表成功", permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询权限列表失败");
        }
    }

    @RequestMapping("findPermissionByRoleId")
    public Result findPermissionByRoleId(@RequestParam("id") Integer id) {
        try {
            List<Integer> ids = permissionService.findPermissionByRoleId(id);
            return new Result(true, "查询角色引用权限成功", ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色引用权限失败");
        }
    }

    @RequestMapping("updatePermission")
    public Result updatePermission(@RequestBody Permission permission) {
        try {
            permissionService.updatePermission(permission);
            return new Result(true, "更新数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "更新数据失败");
        }
    }

}

/***
 *                    .::::.
 *                  .::::::::.
 *                 ::::::::::: 
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 */